<template>
  <div class="main">
    <div class="form_content">
      <NScrollbar>
        <NSpin :show="spinShow">
          <div class="content">
            <div
              class="topTitle"
              v-if="!flat"
            >
              {{ props.title }}
            </div>
            <VDisOrEnd
              v-if="flat"
              :id="collationInfo.workflowInfoVo?.fromId"
              :assigneeFlag="collationInfo.workflowInfoVo?.assigneeFlag"
              title="数字版本详情"
            ></VDisOrEnd>
            <!-- </div> -->
            <!-- 会议预定详情 -->
            <div
              class="basin-info"
              v-if="props.type === '1'"
            >
              <div class="basin-left">
                <div class="box">
                  <span>类型：</span>
                  <span>{{ collationInfo.key1 }}</span>
                </div>
                <div class="box">
                  <span>资产识别符/条码号：</span>
                  <span>{{ collationInfo.key2 }}</span>
                </div>
                <div class="box">
                  <span>版本名称：</span>
                  <span>{{ collationInfo.key3 }}</span>
                </div>
                <div class="box">
                  <span>中华版本收藏主体名称：</span>
                  <span>{{ collationInfo.key4 }}</span>
                </div>
                <div class="box">
                  <span>内容描述：</span>
                  <span>{{ collationInfo.key5 }}</span>
                </div>
                <div class="box">
                  <span>密级：</span>
                  <span>{{ collationInfo.key6 }}</span>
                </div>
                <div class="box">
                  <span>计算机文件大小：</span>
                  <span>{{ collationInfo.key7 }}</span>
                </div>
                <div class="box">
                  <span>格式名称：</span>
                  <span>{{ collationInfo.key8 }}</span>
                </div>
                <div class="box">
                  <span>著录者：</span>
                  <span>{{ collationInfo.key9 }}</span>
                </div>
                <div class="box">
                  <span>备注：</span>
                  <span>{{ collationInfo.key10 }}</span>
                </div>
              </div>
              <div class="basin-right">
                <div class="box">
                  <span>著录类型：</span>
                  <span>{{ collationInfo.key11 }}</span>
                </div>
                <div class="box">
                  <span>主题词：</span>
                  <span>{{ collationInfo.key12 }}</span>
                </div>
                <div class="box">
                  <span>中华版本收藏主题编码：</span>
                  <span>{{ collationInfo.key13 }}</span>
                </div>
                <div class="box">
                  <span>计算机文件名：</span>
                  <span>{{ collationInfo.key14 }}</span>
                </div>
                <div class="box">
                  <span>保管期限：</span>
                  <span>{{ collationInfo.key15 }}</span>
                </div>
                <div class="box">
                  <span>来源：</span>
                  <span>{{ collationInfo.key16 }}</span>
                </div>
                <div class="box">
                  <span>原始载体：</span>
                  <span>{{ collationInfo.key17 }}</span>
                </div>
                <div class="box">
                  <span>储存地址：</span>
                  <span>{{ collationInfo.key18 }}</span>
                </div>
                <div class="box">
                  <span>实体藏品信息：</span>
                  <div
                    class="box-btn"
                    @click="detailShow = !detailShow"
                  >
                    {{ !detailShow ? '展开详情' : '收起详情' }}
                  </div>
                </div>
                <div class="box">
                  <span>文件数量：</span>
                  <div class="box-right">
                    <span>{{ collationInfo.key20 }}</span>
                    <div
                      class="box-btn"
                      @click="handlePreview"
                    >
                      预览文件
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div
              class="detail-panel"
              v-show="detailShow"
            >
              <div class="topTitle">编目信息区</div>
              <div class="basin-info">
                <div class="basin-left">
                  <div class="box">
                    <span>类型：</span>
                    <span>{{ collationInfo.key1 }}</span>
                  </div>
                  <div class="box">
                    <span>资产识别符/条码号：</span>
                    <span>{{ collationInfo.key2 }}</span>
                  </div>
                  <div class="box">
                    <span>版本类型：</span>
                    <span>{{ collationInfo.key3 }}</span>
                  </div>
                  <div class="box">
                    <span>批次编号：</span>
                    <span>{{ collationInfo.key4 }}</span>
                  </div>
                  <div class="box">
                    <span>征集方式：</span>
                    <span>{{ collationInfo.key5 }}</span>
                  </div>
                  <div class="box">
                    <span>责任单位：</span>
                    <span>{{ collationInfo.key6 }}</span>
                  </div>
                  <div class="box">
                    <span>馆藏码：</span>
                    <span>{{ collationInfo.key7 }}</span>
                  </div>
                  <div class="box">
                    <span>流水号：</span>
                    <span>{{ collationInfo.key8 }}</span>
                  </div>
                </div>
                <div class="basin-right">
                  <div class="box">
                    <span>著录类型：</span>
                    <span>{{ collationInfo.key11 }}</span>
                  </div>
                  <div class="box">
                    <span>主题词：</span>
                    <span>{{ collationInfo.key12 }}</span>
                  </div>
                  <div class="box">
                    <span>版本类型说明：</span>
                    <span>{{ collationInfo.key13 }}</span>
                  </div>
                  <div class="box">
                    <span>批次名称：</span>
                    <span>{{ collationInfo.key14 }}</span>
                  </div>
                  <div class="box">
                    <span>交送单位：</span>
                    <span>{{ collationInfo.key15 }}</span>
                  </div>
                  <div class="box">
                    <span>移交人：</span>
                    <span>{{ collationInfo.key16 }}</span>
                  </div>
                  <div class="box">
                    <span>资产码：</span>
                    <span>{{ collationInfo.key17 }}</span>
                  </div>
                  <div class="box">
                    <span>排架号：</span>
                    <span>{{ collationInfo.key18 }}</span>
                  </div>
                </div>
              </div>
            </div>
            <div
              class="detail-panel"
              v-show="detailShow"
            >
              <div class="topTitle">标识区</div>
              <div class="basin-info">
                <div class="basin-left">
                  <div class="box">
                    <span>ISBN号(010-Sa)：</span>
                    <span>{{ collationInfo.key1 }}</span>
                  </div>
                  <div class="box">
                    <span>定价(010-sd)：</span>
                    <span>{{ collationInfo.key2 }}</span>
                  </div>
                  <div class="box">
                    <span>国家书目号(020-sb)：</span>
                    <span>{{ collationInfo.key3 }}</span>
                  </div>
                </div>
                <div class="basin-right">
                  <div class="box">
                    <span>装订方式(010-sb)：</span>
                    <span>{{ collationInfo.key11 }}</span>
                  </div>
                  <div class="box">
                    <span>国家代码(020-Sa)：</span>
                    <span>{{ collationInfo.key12 }}</span>
                  </div>
                  <div class="box">
                    <span>统-书刊号(091-Sa)：</span>
                    <span>{{ collationInfo.key13 }}</span>
                  </div>
                </div>
              </div>
            </div>

            <!-- <div class="topTitle">审批信息</div>
            <div class="approve">
              <div class="box">
                <span>审批状态</span>
                <span style="font-size: 20rem">{{ collationInfo.workflowInfoVo?.status }}</span>
              </div>
              <div class="box">
                <span>审批流程</span>
                <div style="color: #5fb17f; font-size: 20rem; font-weight: 500">
                  <div
                    style="display: inline"
                    v-for="(item, index) in collationInfo.workflowInfoVo?.workflowList"
                    :key="index"
                  >
                    <span :style="{ textDecoration: item.value ? 'underline' : 'none' }">
                      {{ item.key }}
                    </span>
                    <span v-if="index != collationInfo.workflowInfoVo?.workflowList.length - 1">
                      →
                    </span>
                  </div>
                </div>
              </div>
            </div>
            <div class="topTitle">审批记录</div>
            <div class="table">
              <NDataTable
                :bordered="false"
                :columns="columns"
                :data="tableData"
                :pagination="false"
                striped
                :rowKey="rowKey"
              />
            </div> -->
          </div>
        </NSpin>
      </NScrollbar>
    </div>
  </div>
  <FixedAction>
    <div style="display: flex">
      <NSpace
        justify="start"
        style="flex: 0 0 42%"
      >
        <NButton
          style="width: 78px; color: #5fb17f"
          ghost
          @click="goBack"
        >
          <template #icon>
            <img
              :src="VBackImg"
              style="width: 25rem; height: 25rem"
            />
          </template>
          返回
        </NButton>
      </NSpace>

      <NSpace justify="center">
        <NButton
          style="width: 78px; color: #5fb17f"
          type="primary"
          v-if="collationInfo.status == 2 && userId == collationInfo.userId"
          ghost
          @click="handleFile"
        >
          归档
        </NButton>
      </NSpace>
    </div>
  </FixedAction>
  <PreviewModal v-model:show="previewModalVisible" />
</template>

<script setup lang="ts">
import { NScrollbar, NDataTable, useMessage, NSpin, NSpace, NButton, NImage } from 'naive-ui'
import type { DataTableColumns } from 'naive-ui'
import { useRoute } from 'vue-router'
import { ref, watch } from 'vue'
import { sendRequest } from '@/utils/api'
import VAttachments from '@/views/OA/Common/Attachments.vue'
import VDisOrEnd from '@/views/OA/Common/DisOrEnd.vue'
import FixedAction from '@/components/FixedAction.vue'
import VBackImg from '@/assets/images/back.png'
import router from '@/router'
import { useTagStore } from '@/stores/layout'
import PreviewModal from './PreviewModal.vue'
import { RecordType } from '@/router/utils'
import {
  SealApplication_ContractTypeList,
  SealApplication_RelatedMoney,
  SealApplication_MoneyTypeOptions
} from '@/enums/index'

const route = useRoute()
const message = useMessage()
const tags = useTagStore().tags
const props = defineProps({
  type: { type: String }, // 1：用章详情，2：台账详情，3：用章确认详情
  title: { type: String, default: () => '用章详情' },
  id: { type: [Number, String], default: () => null }
})

const detailShow = ref(false)

const previewModalVisible = ref(false)

const handlePreview = () => {
  previewModalVisible.value = true
}

const rowKey: any = (row: any) => row.id || ''
const columns: DataTableColumns = [
  {
    title: '类型',
    key: 'type',
    width: 50
  },
  {
    title: '人员',
    key: 'userName',
    width: 50
  },
  {
    title: '时间',
    key: 'time',
    width: 100
  },
  {
    title: '结果',
    key: 'result',
    width: 50
  },
  {
    title: '备注',
    key: 'remark',
    width: 150
  }
]
// 返回
const goBack = () => {
  const index = tags.findIndex((item: any) => item.path === router.currentRoute.value.fullPath)

  tags.splice(index, 1)
  if (tags.length == 0) {
    router.push({ path: '/OA/Workbench/UserDesktop' })
  } else history.back()
}

const renderContract = () => {
  if (!collationInfo.value.contract) {
    return '-'
  }
  const re = SealApplication_ContractTypeList.find((l) => {
    return l.value === collationInfo.value.contract
  })
  return re ? re.label : '-'
}
const renderRelatedMoney = () => {
  if (!collationInfo.value.relatedMoney) {
    return '-'
  }
  const re = SealApplication_RelatedMoney.find((l) => {
    return l.value === collationInfo.value.relatedMoney
  })
  return re ? re.label : '-'
}
const renderMoneyType = () => {
  if (!collationInfo.value.moneyType) {
    return '-'
  }
  const re = SealApplication_MoneyTypeOptions.find((l) => {
    return l.value === collationInfo.value.moneyType
  })
  return re ? re.label : '-'
}
/** 是否是 合同章（原A0508） */
const isContractSeal = () => {
  if (
    !collationInfo.value ||
    Object.keys(collationInfo).length === 0 ||
    !(collationInfo as any).value.chapterCategoryCode
  ) {
    return false
  }
  return ['A0560', 'A0561', 'A0562', 'A0563', 'A0564', 'A0565', 'A0566'].includes(
    (collationInfo as any).value.chapterCategoryCode
  )
}
/** 是否是 法人章（原A0507） */
const isCorporateSeal = () => {
  if (
    !collationInfo.value ||
    Object.keys(collationInfo).length === 0 ||
    !(collationInfo as any).value.chapterCategoryCode
  ) {
    return false
  }
  return (collationInfo as any).value.chapterCategoryCode === 'A0507'
}
// 获取详情
const spinShow: any = ref(false)
const collationInfo = ref<any>({
  key1: 'xxxxxxxxxxxxxxxxxxx',
  key2: 'xxxxxxxxxxxxxxxxxxx',
  key3: 'xxxxxxxxxxxxxxxxxxx',
  key4: 'xxxxxxxxxxxxxxxxxxx',
  key5: 'xxxxxxxxxxxxxxxxxxx',
  key6: 'xxxxxxxxxxxxxxxxxxx',
  key7: 'xxxxxxxxxxxxxxxxxxx',
  key8: 'xxxxxxxxxxxxxxxxxxx',
  key9: 'xxxxxxxxxxxxxxxxxxx',
  key10: 'xxxxxxxxxxxxxxxxxxx',
  key11: 'xxxxxxxxxxxxxxxxxxx',
  key12: 'xxxxxxxxxxxxxxxxxxx',
  key13: 'xxxxxxxxxxxxxxxxxxx',
  key14: 'xxxxxxxxxxxxxxxxxxx',
  key15: 'xxxxxxxxxxxxxxxxxxx',
  key16: 'xxxxxxxxxxxxxxxxxxx',
  key17: 'xxxxxxxxxxxxxxxxxxx',
  key18: 'xxxxxxxxxxxxxxxxxxx',
  key19: 'xxxxxxxxxxxxxxxxxxx',
  key20: 'xxxxxxxxxxxxxxxxxxx'
})
const tableData: any = ref([])
const getReturnInfo = async () => {
  spinShow.value = true
  const [error, res] = await sendRequest({
    module: 'meeting',
    api: 'roomReservationDetail',
    uri: {
      id: props?.id as never
    }
  })
  if (error) return message.error(error.message)
  if (res.code !== 0) return message.error(res.msg)
  collationInfo.value = res.data
  tableData.value = res.data?.workflowInfoVo?.approvalRecordsList || []
  spinShow.value = false
}

//获取用户信息
const userId: any = ref('')
const getUserInfo = async () => {
  const [error, res] = await sendRequest({
    module: 'seal',
    api: 'userInfo'
  })
  if (error) return message.error(error.message)
  if (res.code !== 0) return message.error(res.msg)
  userId.value = res.data?.id
}
getUserInfo()

//归档
const fileVisiable = ref(false)
const modalId: any = ref()
function handleFile() {
  fileVisiable.value = true
  modalId.value = props.id || route.query.id
}

const flat: any = ref(false)

watch(
  () => route,
  async () => {
    if (
      (route.path == '/OA/Meeting/RoomReservationDetail' ||
        route.path == '/OA/Seal/SealMakeSureDetail' ||
        route.path == '/OA/Seal/LedgerDetail') &&
      (props.id || route.query.id)
    ) {
      await getReturnInfo()
    }
    if (route.query.flat) flat.value = route.query.flat
  },
  { deep: true, immediate: true }
)
</script>

<style scoped lang="scss">
@mixin CommonFont {
  font-size: 18rem;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  color: #333333;
}

.main {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  overflow: hidden;

  .form_content {
    width: 100%;
    height: 100%;
    padding: 20rem 20rem 0;

    .topTitle {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 22px;
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      color: #5fb17f;
      margin-bottom: 10rem;
    }

    .meeting-approval {
      display: flex;
      gap: 20px;
    }

    .content {
      display: flex;
      flex-direction: column;
      width: 100%;
      height: 100%;
      padding: 18rem 33rem 120rem;
      overflow-y: scroll;
      background-color: #ebeceb;
      @include CommonFont;
      .detail-panel {
        margin-top: 10px;
      }

      .basin-info {
        display: flex;
        width: 100%;
        height: auto;
        border-bottom: 1px dashed #bc9e70;
        margin-bottom: 10rem;

        .basin-left {
          flex: 0 0 40%;
        }

        .box {
          display: flex;
          align-items: center;
          gap: 19px;
          margin-bottom: 34px;
          font-size: 16rem;
          &-right {
            display: flex;
            align-items: center;
            justify-content: flex-start;
            gap: 19px;
          }
          &-btn {
            display: flex;
            width: 72px;
            height: 30px;
            padding: 2px 4px;
            justify-content: center;
            align-items: center;
            border-radius: 2px;
            border: 1px solid #0b8d61;
            color: #0b8d61;
            font-size: 14px;
            line-height: 22px; /* 157.143% */
            cursor: pointer;
          }

          > span:first-child {
            width: 180px;
            text-align: right;
            color: #4e5969;
            text-align: right;
            font-size: 14px;
            font-weight: 400;
            line-height: 22px; /* 157.143% */
          }

          span:last-child {
            color: #1d2129;
            font-variant-numeric: lining-nums tabular-nums;
            font-size: 14px;
            font-weight: 600;
            line-height: 22px; /* 157.143% */
          }
        }

        .box:last-child {
          margin-bottom: 20rem;
        }
      }

      .approve {
        display: flex;
        width: 100%;
        margin-bottom: 20rem;
        border-bottom: 1px dashed #bc9e70;

        .box {
          display: flex;
          flex-direction: column;
          justify-content: center;
          min-width: 100rem;
          width: auto;
          height: 90rem;
          border-radius: 4px;
          border: 1px solid #d9d9d9;
          padding: 12rem;
          margin-right: 10rem;
          margin-bottom: 20rem;
        }
      }

      .table {
        width: 80%;
        background-color: #e1e1e1;
      }
    }
  }
}

.content::-webkit-scrollbar {
  display: none;
}

:deep(.n-spin-container) {
  width: 100%;
  height: 100%;
}

:deep(.n-scrollbar-container) {
  width: 100%;
  height: 100%;
}

:deep(.n-scrollbar-content) {
  width: 100%;
  height: 100%;
}

:deep(.n-spin-content) {
  width: 100%;
  height: 100%;
}
</style>
